<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>球队列表</title>
    <script type="text/javascript" src="/js/jquery-1.11.1.js"></script>
    <script type="text/javascript" src="/js/bootstrap.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.css"></link>
    <link rel="stylesheet" type="text/css" href="/css/style.css"></link>
    <style>
        span {
            color: #1EABFF;
            padding: 0 5px;
        }
    </style>
</head>
<body>
<form id="myForm" class="form-horizontal">
    <h2>NBA球队列表</h2>
    <div class="searchDiv">
        <button class="btn btn-danger">批量删除</button>
        <a href="/pages/team/add.html" class="btn btn-success">添加</a>
        球队名称:<input name="teamName" placeholder="英文名称"/>
        中文名称:<input name="chineseName" placeholder="中文名称"/>
        教练:<input name="coach" placeholder="教练姓名"/>
        创建时间:<input name="beginDate" type="date"/>-<input name="endDate" type="date"/>
        所属联盟:<select name="area">
        <option value="-1">请选择联盟</option>
        <option value="0">东部联盟</option>
        <option value="1">西部联盟</option>
    </select>
        <button type="button" onclick="loadData()" class="btn btn-info">查询</button>
        <button type="reset" class="btn btn-warning">重置</button>
    </div>
    <table class="table table-bordered table-striped">
        <thead>
        <tr>
            <th>全选<input type="checkbox" id="all"/></th>
            <th>球队ID</th>
            <th>球队logo</th>
            <th>球队名称</th>
            <th>中文名称</th>
            <th>主教练</th>
            <th>主场馆</th>
            <th>所属地区</th>
            <th>成立时间</th>
            <th>所属联盟</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tboby"></tbody>
    </table>
    <!--分页部分-->
    <div class="form-group">
        <div class="col-sm-3" style="text-align: right">
            <ul class="pagination" style="margin: 0;">
                <li name="pageLi" id="firstPage"><a href="javascript:void(0)">首页</a></li>
                <li name="pageLi" id="prePage"><a href="javascript:void(0)">上一页</a></li>
                <li name="pageLi" id="nextPage"><a href="javascript:void(0)">下一页</a></li>
                <li name="pageLi" id="lastPage"><a href="javascript:void(0)">末页</a></li>
            </ul>
        </div>
        <label class="control-label col-sm-3" style="text-align: right">
            共<span id="total"></span>条数据，共<span id="pages"></span>页
        </label>
        <label class="control-label col-sm-1" style="text-align: right">
            每页显示
        </label>
        <div class="col-sm-1" style="text-align: right; padding:0;margin: 0">
            <select class="form-control" name="pageSize" id="pageSize">
                <option value="5">5</option>
                <option value="10">10</option>
                <option value="15">15</option>
            </select>
        </div>
        <label class="control-label col-sm-1" style="text-align: left;width: 87px;padding: 8px 5px;">
            条数据
        </label>
        <label class="control-label col-sm-1" style="text-align: right">
            跳转到第
        </label>
        <div class="col-sm-1" style="text-align: right; padding:0;margin: 0"><input class="form-control" id="pageNum" name="pageNum"
                                                                                    value="1"/></div>
        <label class="control-label col-sm-1" style="text-align: left">页</label>
    </div>
</form>
</body>

</html>
<script>
    //页面加载完毕之后发送异步请求
    $(function () {
        let url = document.location.toString();
        console.log(url);
        let pageNum = url.GetValue("pageNum");
        let pageSize = url.GetValue("pageSize");
        if (pageNum != null) {
            $("input[name='pageNum']").val(pageNum);
        }
        if (pageSize != null) {
            $("select[name='pageSize']").val(pageSize);
        }
        loadData();
        $("ul[class='pagination']>li").click(function () {
            if (($(this).attr("class")) == 'disabled')
                return;
            let num = $(this).attr("name");
            $("#pageNum").val(num);
            loadData();
        });
    });


    //填充分页信息
    function fillPageData(pageInfo) {
        console.log(pageInfo.prePage,pageInfo.nextPage,pageInfo.pages);
        //填充信息
        $("#total").html(pageInfo.total);
        $("#pages").html(pageInfo.pages);
        $("input[name='pageNum']").val(pageInfo.pageNum);
        $("select[name='pageSize']").val(pageInfo.pageSize);
        //设置按钮的属性值和样式
        $("#firstPage").attr("name", 1);
        $("#prePage").attr("name", pageInfo.prePage);
        $("#nextPage").attr("name", pageInfo.nextPage);
        $("#lastPage").attr("name", pageInfo.pages);
        $("#firstPage").removeClass("disabled");
        $("#prePage").removeClass("disabled");
        $("#nextPage").removeClass("disabled");
        $("#lastPage").removeClass("disabled");
        if (pageInfo.isFirstPage) {//如果是第一页：上一页和首页禁用
            $("#firstPage").addClass("disabled");
            $("#prePage").addClass("disabled");
        }
        if (pageInfo.isLastPage) {//如果已经是最后一页：下一页和末页禁用
            $("#nextPage").addClass("disabled");
            $("#lastPage").addClass("disabled");
        }
    }

    //更新按钮
    function updateTeam(id) {
        let pageNum = $("#pageNum").val();
        let pageSize = $("#pageSize").val();
        window.location.href = "/pages/team/update.html?teamId=" + id + "&pageNum=" + pageNum + "&pageSize=" + pageSize;
    }

    // 删除按钮
    function deleteTeam(id) {
        if (confirm("确定要删除吗?")) {
            //发起异步请求
            $.ajax({
                type: "POST",
                url: "/team/" + id, //RESTful风格的API定义
                data: "_method=DELETE",
                success: function (vo) {
                    if (vo.code == 200) {
                        loadData();
                    } else {
                        alert("删除失败！" + vo.msg);
                    }
                }
            });
        }
    }

    //上传图片按钮
    function uploadLogo(id) {
        let pageNum = $("input[name='pageNum']").val();
        let pageSize = $("select[name='pageSize']").val();
        window.location.href = "/pages/team/uploadLogo.html?teamId=" + id + "&pageNum=" + pageNum + "&pageSize=" + pageSize;
    }

    String.prototype.GetValue = function (para) {
        let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?") + 1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    //加载数据
    function loadData() {
        $.ajax({
            type: "GET",
            url: "/team/list",
            data: $("#myForm").serialize(),//主要是分页和多条件参数数据
            dataType: "json",
            success: function (vo) {
                let list = vo.pageInfo.list;
                let str = "";
                for (let i = 0; i < list.length; i++) {
                    let obj = list[i];
                    let teamId = obj.teamId;
                    let teamLogo = obj.teamLogo == null ? '' : '<img  src="/img/uploadLogo/' + obj.teamLogo + '">';
                    let teamName = obj.teamName == null ? '' : obj.teamName;
                    let chineseName = obj.chineseName == null ? '' : obj.chineseName;
                    let coach = obj.coach == null ? '' : obj.coach;
                    let stadium = obj.stadium == null ? '' : obj.stadium;
                    let location = obj.location == null ? '' : obj.location;
                    let time = obj.createTime == null ? '' : obj.createTime.split('-')[0];
                    let area = obj.area == '0' ? "东部联盟" : "西部联盟";
                    let isDel = obj.isDel;
                    let btnStr = "";
                    if (isDel == 0) {
                        btnStr = '<button type="button" name="deleteBtn" class="btn btn-sm btn-danger" onclick="deleteTeam(' + teamId + ')">删除</button>&nbsp;' +
                            '<button type="button" name="updateBtn" class="btn btn-sm btn-info" onclick="updateTeam(' + teamId + ')">更新</button>&nbsp;' +
                            '<button type="button" name="uploadBtn" class="btn btn-sm btn-info" onclick="uploadLogo(' + teamId + ')">上传图片</button>';
                    } else {
                        btnStr = '<button name="deleteBtn" class="btn btn-sm btn-danger disabled">已删除</button>';
                    }
                    str += '<tr>' +
                        '            <td><input type="checkbox"/></td>' +
                        '            <td>' + teamId + '</td>' +
                        '            <td>' + teamLogo + '</td>' +
                        '            <td>' + teamName + '</td>' +
                        '            <td>' + chineseName + '</td>' +
                        '            <td>' + coach + '</td>' +
                        '            <td>' + stadium + '</td>' +
                        '            <td>' + location + '</td>' +
                        '            <td>' + time + '</td>\n' +
                        '            <td>' + area + '</td>\n' +
                        '            <td>' + btnStr + '</td>' +
                        '</tr>'
                }
                $("#tboby").html(str);
                //填充分页数据
                fillPageData(vo.pageInfo);
            }
        });
    }
</script>
